package com.data.open;

import java.awt.FileDialog;
import java.io.File;

import javax.swing.JFrame;

import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.map.FeatureLayer;
import org.geotools.map.Layer;
import org.geotools.map.MapContent;
import org.geotools.styling.SLD;
import org.geotools.styling.Style;
import org.geotools.swing.JMapFrame;

public class OpenShp {
	public static void main(String[] args) throws Exception {
		String path,name,fileChosen;
        // display a data store file chooser dialog for shapefiles
        //File file = JFileDataStoreChooser.showOpenFile("shp", null);
		//JFileChooser file = new JFileChooser();
		FileDialog fileDialog = new FileDialog(new JFrame(), "Open MXD|MXT|PMF file", FileDialog.LOAD);
		fileDialog.setVisible(true);
		path = fileDialog.getDirectory();
		name = fileDialog.getFile();
		if (!(path != null && name != null && path.length() > 0 && name.length() > 0)) {
			// Nothing chosen
			return;
		}
		fileChosen = path + File.separator + name;
        File file=new File(fileChosen);

        FileDataStore store = FileDataStoreFinder.getDataStore(file);
        SimpleFeatureSource featureSource = store.getFeatureSource();

        // Create a map content and add our shapefile to it
        MapContent map = new MapContent();
        map.setTitle("Quickstart");
       
        Style style = SLD.createSimpleStyle(featureSource.getSchema());
        Layer layer = new FeatureLayer(featureSource, style);
        map.addLayer(layer);

        // Now display the map
        JMapFrame.showMap(map);
    }
}
